<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class addpengajuan extends CI_Model {
	function __construct()
	{
		parent::__construct();
	}

	 function add_pengajuan($id_user){
	$datelawas = $this->input->post('tanggal_permohonan');
		/* $tanggal=$date->format('Y-m-d'); */
		$tanggal = date("Y-m-d", strtotime($datelawas));
		
		$q = $this->db->query('SELECT ID_PEMOHON FROM pemohon WHERE ID_USER = ?',array($id_user));
		$data = array_shift($q->result_array());
		$datas = $data['ID_PEMOHON'];
		
		$data=array(
			'ID_PEMOHON' => $datas,
			'TGL_PENGAJUAN'=>$tanggal,
			'ALASAN' => $this->input->post('alasan'),
			'LEMBAR_TK1'=>$this->input->post('account1'),
			'LEMBAR_TK2'=>$this->input->post('account2'),
			'LEMBAR_TK3'=>$this->input->post('account3'),
			'VERIFIKATOR'=>$this->input->post('account4'),
			'STATUS'=>'1'
		  );
		$this->db->insert('pengajuan',$data);
  }
	 
	public function add_pemohon($username)
	{
		$datelawas = $this->input->post('tanggal_lahir');
		
			$tanggal = date("Y-m-d", strtotime($datelawas));

			$q = $this->db->query('SELECT ID_USER FROM user WHERE USERNAME = ?',array($username));
		$data = array_shift($q->result_array());
		$datas = $data['ID_USER'];
		
		
			$datass=array(
				
				
				'TEMPAT_LAHIR'=>$this->input->post('tempat_lahir'),
				'TGL_LAHIR'=>$tanggal,
				'JENIS_KELAMIN'=>$this->input->post('jenis_kelamin'),
				'ALAMAT'=>$this->input->post('alamat'),
				'TELP'=>$this->input->post('nomor_telepon'),
				'HP'=>$this->input->post('hp')
			  );
			

			$this->db->where('ID_USER', $datas);
			$this->db->update('pemohon', $datass);
		
		/* $id_peg = $this->selectWithParam('pemohon', Array('NAMA_LENGKAP' => $this->input->post('nama_lengkap')));
		
		$data = array(
				   'ID_PEMOHON' => $id_peg[0]['ID_PEMOHON']
				);

		$this->db->where('ID_USER', $id_user[0]['ID_USER']);
		$this->db->update('user', $data); */

		/* $sql = "INSERT INTO user (STATUS) VALUES('1')";
		$this->db->query($sql); */
	}

	public function selectAll($first, $last){
		$user_id = $this->session->userdata('id_user');
		$this->db->select('u.ID_PENGAJUAN, u.TGL_PENGAJUAN, u.TGL_PENGAMBILAN, u.LEMBAR_TK1, u.LEMBAR_TK2, u.LEMBAR_TK3, p.TK1, p.TK2, p.TK3, u.STATUS');
		$this->db->from('pengajuan u, pemohon p');
		$this->db->where('u.ID_PEMOHON = p.ID_PEMOHON');
		$this->db->where("p.ID_USER = $user_id");
		$this->db->offset($first);
		$query = $this->db->get(0, $last);
				
		return $query->result_array();
	}
	
	public function pilihSemua($first, $last){
	
		$this->db->select('u.ID_PENGAJUAN, u.TGL_PENGAJUAN, u.TGL_PENGAMBILAN, u.LEMBAR_TK1, u.LEMBAR_TK2, u.LEMBAR_TK3 ,P.NAMA_LENGKAP, p.TK1, p.TK2, p.TK3, u.STATUS');
		$this->db->from('pengajuan u, pemohon p');
		$this->db->where('u.ID_PEMOHON = p.ID_PEMOHON');
		
		$this->db->offset($first);
		$query = $this->db->get(0, $last);
				
		return $query->result_array();
	}
	

	public function selectUserByID($idPengajuan)
	{
		$this->db->select('p.NAMA_LENGKAP, p.TEMPAT_LAHIR, p.TGL_LAHIR, p.JENIS_KELAMIN, p.ALAMAT, p.TELP, p.TK1, p.TK2, p.TK3, p.ALAMAT_TK1, p.ALAMAT_TK2, p.ALAMAT_TK3, p.ALAMAT_KTP, p.HP, u.TGL_PENGAJUAN, u.ALASAN, u.LEMBAR_TK1, u.LEMBAR_TK2, u.LEMBAR_TK3, u.VERIFIKATOR, u.STATUS');
		$this->db->from('pengajuan u, pemohon p');
		$this->db->where('u.ID_PEMOHON = p.ID_PEMOHON');
		$this->db->where("u.ID_PENGAJUAN = $idPengajuan");
		$query = $this->db->get();
		
		return $query->result_array();
	}

	public function selectPengajuanByID($idPengajuan)
	{	
		$this->db->select('s.EMAIL, p.NAMA_LENGKAP, p.TEMPAT_LAHIR, p.TGL_LAHIR, p.JENIS_KELAMIN, p.ALAMAT, p.TELP, p.TK1, p.TK2, p.TK3, p.ALAMAT_TK1, p.ALAMAT_TK2, p.ALAMAT_TK3, p.ALAMAT_KTP, p.HP, u.TGL_PENGAJUAN, u.ALASAN, u.LEMBAR_TK1, u.LEMBAR_TK2, u.LEMBAR_TK3, u.VERIFIKATOR, u.STATUS');
		$this->db->from('pengajuan u, pemohon p, user s');
		$this->db->where('s.ID_PEMOHON = u.ID_PEMOHON');
		$this->db->where('u.ID_PEMOHON = p.ID_PEMOHON');
		$this->db->where("u.ID_PENGAJUAN = $idPengajuan");
		$query = $this->db->get();
		
		return $query->result_array();
	}

	public function selectPemohonByID($idUser)
	{
		$this->db->select('*');
		$this->db->from('pemohon');
		$this->db->where('id_user', $idUser);
		$query = $this->db->get();

		return $query->result_array();
	}

	public function selectWithParam($table, $param)
	{
		$query = $this->db->get_where($table, $param);
		return $query->result_array();
	}
	 
	/* public function edit()
	{

		$datelawas = $this->input->post('tanggal_lahir');
		 $tanggal=$date->format('Y-m-d'); 
		$tanggal = date("Y-m-d", strtotime($datelawas));
		
		$peg = array(
					'NAMA_LENGKAP'	=> $this->input->post('nama_lengkap'),
					'NIP'			=> $this->input->post('NIP'),
					'TEMPAT_LAHIR'	=> $this->input->post('tempat_lahir'),
					'TGL_LAHIR'		=> $tanggal,
					'JENIS_KELAMIN'	=> $this->input->post('jenisk'),
					'ALAMAT'		=> $this->input->post('alamat'),
					'TELP'			=> $this->input->post('nomor_telepon'),
					'HP'			=> $this->input->post('hp')
				);

		$this->db->where('ID_USER', $this->input->post('id_user'));
		$this->db->update('pegawai', $peg);
		
		if($this->input->post('password') == '*********'){
			$usr = array(
						'ROLE'		=> $this->input->post('role'),
						'STATUS'	=> $this->input->post('status')
					);
		}
		else{
			$usr = array(
						'ROLE'		=> $this->input->post('role'),
						'STATUS'	=> $this->input->post('status'),
						'PASSWORD'=>md5($this->input->post('password'))
					);
		}
		$this->db->where('ID_USER', $this->input->post('id_user'));
		$this->db->update('user', $usr);
	} */
	
	public function getTotalRecords()
	{
		$this->db->from('pengajuan u, pemohon p');
		$this->db->where('u.ID_PEMOHON = p.ID_PEMOHON');
		$query = $this->db->get();
		
		return $query->num_rows();
	}
	
	public function alamat($path,$id_user)
	{
		$username = $this->session->userdata('user_name');
	//$name = $_FILES['userfile']['name'];
		$datapemohon = $this->frontmodel->get_pemohon($username);
		
		foreach($datapemohon as $row){
		$k1 = $row->ALAMAT_TK1;
		$k2 = $row->ALAMAT_TK2;
		$k3 = $row->ALAMAT_TK3;
		$k4 = $row->ALAMAT_KTP;
		}
		
			if (empty($this->input->post('file_1'))){
			
			$tk1 = $k1;
			}
			else{
			$tk1 = $path[1];
			}
			
			if (empty($this->input->post('file_2'))){
			
			$tk2 = $k2;
			}
			else{
			$tk2 = $path[2];
			}
			
			if (empty($this->input->post('file_1'))){
			
			$tk3 = $k3;
			}
			else{
			$tk3 = $path[3];
			}
			
			if (empty($this->input->post('file_0'))){
			
			$ktp = $k4;
			}
			else{
			$ktp = $path[0];
			}
			
		$data=array(
			'ALAMAT_TK1'=>$tk1,
			'ALAMAT_TK2'=>$tk2,
			'ALAMAT_TK3'=>$tk3,
			'ALAMAT_KTP'=>$ktp
		);
		$this->db->where('ID_USER',$id_user);
		$this->db->update('pemohon',$data);
		//echo $pathh;
	}

	public function verifikasiPengajuan($id_pengajuan){
		date_default_timezone_set('Asia/Jakarta');
		$data=array(
			'TGL_VERIFIKASI' => date('Y-m-d'),
			'STATUS' => 2
		);

		$this->db->where('ID_PENGAJUAN', $id_pengajuan);
		$this->db->update('pengajuan', $data);
	}

	public function verifikasiLegalisir($id_pengajuan){
		date_default_timezone_set('Asia/Jakarta');
		$data=array(
			'TGL_VERIFIKASI' => date('Y-m-d'),
			'STATUS' => 3
		);

		$this->db->where('ID_PENGAJUAN', $id_pengajuan);
		$this->db->update('pengajuan', $data);
	}

	public function persetujuanLegalisir($id_pengajuan){
		date_default_timezone_set('Asia/Jakarta');
		$data=array(
			'TGL_VERIFIKASI_KADIN' => date('Y-m-d'),
			'STATUS' => 3
		);

		$this->db->where('ID_PENGAJUAN', $id_pengajuan);
		$this->db->update('pengajuan', $data);
	}
}

?>